package com.ag.lwc.test;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.ag.bean.TbRole;
import com.ag.bean.TbUser;
import com.ag.bean.TbUserRole;
import com.ag.dao.TbRoleDAO;

public class InitRole {

	public void init()
	{
		ApplicationContext context = new FileSystemXmlApplicationContext(new String[]{ "classpath:/applicationContext.xml",
				"classpath:/applicationContext-dgm.xml", 
				"classpath:/applicationContext-hmk.xml",
				"classpath:/applicationContext-lzw.xml",
				"classpath:/applicationContext-lwc.xml",
				"classpath:/applicationContext-lzm.xml",
				"classpath:/applicationContext-wsq.xml",
				"classpath:/applicationContext-wzp.xml",
				"classpath:/applicationContext-zhh.xml",
				"classpath:/applicationContext-zlw.xml",
				"classpath:/applicationContext-zrt.xml",});
		TbRoleDAO roledao =(TbRoleDAO) context.getBean("TbRoleDAO");
		TbRole role = null;
		Transaction tx = null;
		Session session = roledao.getSessionFactory().openSession();
		try
		{
			tx = session.beginTransaction();
			//加载要被初始化的角色
			role =(TbRole) session.get(TbRole.class, "1017");
			System.out.println("角色id："+role.getRId()+" \t角色："+role.getRName());		
			List<TbUser> users = session.createQuery("from TbUser").list();
			int size=0;
			for(TbUser user:users)
			{
				//如果该用户没有角色
				if(user.getTbUserRoles().size()==0)
				{
					size=size+1;
					System.out.print(user.getUId()+"\t"+user.getUName()+"\t");
					//新增一个用户角色
					TbUserRole userrole = new TbUserRole();
					userrole.setTbUser(user);
					userrole.setTbRole(role);
					userrole.setA("0");
					user.getTbUserRoles().add(userrole);		//加入到用户的用户角色当中
					role.getTbUserRoles().add(userrole);		//加入到角色的用户角色当中	
					
				}
				
			}
			System.out.println("\n\n\t\t没有角色的用户的数目："+size);
			tx.commit();
		}
		catch(Exception e)
		{
			tx.rollback();
		}
		finally{
			session.close();
		}
		
		
		
	}
	public void test()
	{
		ApplicationContext context = new FileSystemXmlApplicationContext(new String[]{ "classpath:/applicationContext.xml",
				"classpath:/applicationContext-dgm.xml", 
				"classpath:/applicationContext-hmk.xml",
				"classpath:/applicationContext-lzw.xml",
				"classpath:/applicationContext-lwc.xml",
				"classpath:/applicationContext-lzm.xml",
				"classpath:/applicationContext-wsq.xml",
				"classpath:/applicationContext-wzp.xml",
				"classpath:/applicationContext-zhh.xml",
				"classpath:/applicationContext-zlw.xml",
				"classpath:/applicationContext-zrt.xml",});
		TbRoleDAO roledao =(TbRoleDAO) context.getBean("TbRoleDAO");
		TbRole role = null;
		
		Session session = roledao.getSessionFactory().openSession();
		
	
			//加载要被初始化的角色
			role =(TbRole) session.get(TbRole.class, "1017");
			System.out.println("角色id："+role.getRId()+" \t角色："+role.getRName());		
			List<TbUser> users = session.createQuery("from TbUser").list();
			int size=0;
			for(TbUser user:users)
			{
				System.out.print("\t"+user.getUId()+"\t"+user.getUName());
				//如果该用户没有角色
				if(user.getTbUserRoles().size()>0)
				{
						for(TbUserRole userrole:user.getTbUserRoles())
						{
							System.out.print("\t"+userrole.getTbRole().getRName());
							
						}
					
				}
				System.out.println();
				
			}
			System.out.println("\n\n\t\t没有角色的用户的数目："+size);
		session.close();
		

	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		InitRole test = new InitRole();
		
		test.init();
	//	test.test();

	}

}
